﻿using System;
using System.Collections.Generic;
using System.Linq;
using DeepZoomPivotConstructor.PivotLib;
using HtmlAgilityPack;
using PaperPluginUtilities;
using System.Net;
using System.IO;
using System.Text.RegularExpressions;

namespace ATLASConfNotes
{
    static class ATLASUtilities
    {
        /// <summary>
        /// Parse the ATLAS info web page to get things like date and title
        /// </summary>
        /// <param name="uri"></param>
        /// <returns></returns>
        public static Dictionary<string, object> ParseInfoPage(Uri uri)
        {
            var result = new Dictionary<string, object>();

            WebRequest meta = WebRequest.Create(uri);
            using (var resp = meta.GetResponse())
            {
                using (var outStream = new StreamReader(resp.GetResponseStream()))
                {
                    Regex rg = new Regex(@"^\s*<([^>]+)>\s*$");
                    string line = outStream.ReadLine();
                    do
                    {
                        var match = rg.Match(line);
                        if (match.Success)
                        {
                            var key = match.Groups[1].Value;
                            result[key] = "";
                            bool done = false;
                            while (!done)
                            {
                                line = outStream.ReadLine();
                                done = outStream.EndOfStream;
                                var matchText = rg.Match(line);
                                if (!matchText.Success)
                                {
                                    if (((string)result[key]).Length > 0)
                                    {
                                        result[key] += " ";
                                    }
                                    result[key] += line;
                                }
                                else
                                {
                                    done = true;
                                }
                            }
                        }
                    } while (!outStream.EndOfStream);
                    outStream.Close();
                }
            }

            ///
            /// Fix up the date
            /// 

            if (result.ContainsKey("DATE"))
            {
                Regex dt = new Regex("([^-]+)-([^-]+)-([^-]+)");
                var fdate = dt.Match((string)result["DATE"]);
                if (fdate.Success)
                {
                    result["DATE"] = new DateTime(
                        Convert.ToInt32(fdate.Groups[3].Value),
                        Convert.ToInt32(fdate.Groups[2].Value),
                        Convert.ToInt32(fdate.Groups[1].Value)
                        );
                }
                else
                {
                    try
                    {
                        result["DATE"] = Convert.ToDateTime((string)result["DATE"]);
                    }
                    catch
                    {
                        result["DATE"] = DateTime.Now;
                    }
                }
            }

            return result;
        }
    }
}
